package com.quarantine.web.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.quarantine.model.newsphoto;
import com.quarantine.service.NewsphotoManager;
import com.quarantine.util.DateUtils;
import com.quarantine.web.CrudActionSupport;

@Namespace("/admin")
@Results({
	@Result(name ="save", location = "newsphoto-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"}),
	@Result(name ="delete", location = "newsphoto-admin!list.action", type ="redirect",params={"currentpage","${currentpage}"})
})
public class NewsphotoAdminAction extends CrudActionSupport<newsphoto> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private newsphoto entity;// =new TodayNews();
	private NewsphotoManager newsphotomanager;
	private File pictruepath;
	private String pictruepathFileName;
	private String pictruepathContenttype;
	private String id;
	private String ids;// 逗号隔开id集合
	private List<newsphoto> newsphotoList;
	private String time;
	/**
	 * 分頁用
	 */
	private int currentpage;
	private int pageNo;
	public static final int pageSize = 20;
	private int prePage;
	private long totalPages;
	private int nextPage;
	private long allsize;


	public newsphoto getEntity() {
		return entity;
	}

	public void setEntity(newsphoto entity) {
		this.entity = entity;
	}

	public NewsphotoManager getNewsphotomanager() {
		return newsphotomanager;
	}

	@Autowired
	public void setNewsphotomanager(NewsphotoManager newsphotomanager) {
		this.newsphotomanager = newsphotomanager;
	}

	public File getPictruepath() {
		return pictruepath;
	}

	public void setPictruepath(File pictruepath) {
		this.pictruepath = pictruepath;
	}

	public String getPictruepathFileName() {
		return pictruepathFileName;
	}

	public void setPictruepathFileName(String pictruepathFileName) {
		this.pictruepathFileName = pictruepathFileName;
	}

	public String getPictruepathContenttype() {
		return pictruepathContenttype;
	}

	public void setPictruepathContenttype(String pictruepathContenttype) {
		this.pictruepathContenttype = pictruepathContenttype;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<newsphoto> getNewsphotoList() {
		return newsphotoList;
	}

	public void setNewsphotoList(List<newsphoto> newsphotoList) {
		this.newsphotoList = newsphotoList;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public long getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public long getAllsize() {
		return allsize;
	}

	public void setAllsize(long allsize) {
		this.allsize = allsize;
	}

	
	@Override
	public newsphoto getModel() {
		// TODO Auto-generated method stub
		return null;
	}
/***
 * 后台列表显示
 */
	@Override
	public String list() throws Exception {
		int pageSize = 9;
		Page<newsphoto> page = new Page<newsphoto>();
		page.setAutoCount(true);
		page.setPageNo(currentpage);
		page.setPageSize(pageSize);
	    newsphotomanager.getList(page);
		setNewsphotoList(page.getResult());
		pageNo = page.getPageNo();
		prePage = page.getPrePage();
		nextPage = page.getNextPage();
		totalPages = page.getTotalPages();
		if(totalPages==0){
			
			pageNo=0;
		}
		allsize = page.getTotalCount();
		return "list";
	}
	public String show() {
		entity = newsphotomanager.getById(id);
		 
		return "addnews";
	}public String show1() {
	       time=DateUtils.now();
		 
		return "addnews";
	}
	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		String root = ServletActionContext.getServletContext().getRealPath(
				"/photo");// 获取路径
		
	if(pictruepathFileName!=""&&pictruepathFileName!=null){   //用新的文件上传
		if (StringUtils.isEmpty(id)) {       //id为空 ，即是为新增的元素
			  entity.setClickrate(0);   
		} else {                                       //不为空，即为重编辑内容
			entity = newsphotomanager.getById(id);
			File imgFile = new File(root + "/" + entity.getPicturepath());
			if (imgFile.exists()) {
				imgFile.delete();
			}
		}
		String fname = pictruepathFileName;                  // 重命名
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyyMMddHHmmss");
		java.util.Date mydate = new java.util.Date();
		fname = new String(sdf.format(mydate)
				+ fname.substring(fname.indexOf(".")));
		entity.setPicturepath(fname);                 //将新的文件名字戏写入entity中去 
		InputStream is = new FileInputStream(pictruepath);
		File destFile = new File(root, fname);
		OutputStream os = new FileOutputStream(destFile);
		byte[] buffer = new byte[1024];
		int length = 0;
		while (-1 != (length = is.read(buffer))) // 写入文件
		{
			os.write(buffer, 0, length);
		}
		is.close();
		os.close();
		
	}
	else{                                           //没有上传新图片，直接查找后更改
		System.out.println("dsfsf");
		entity = newsphotomanager.getById(id);
	}
	       
	 
//	    entity.setEditTime(DateUtils.now());
		newsphotomanager.set(entity); // 保存

		return "save";
	}
/**
 * 删除单个图片新闻
 */
	@Override
	public String delete() throws Exception {
		entity = newsphotomanager.getById(id);
		String root = ServletActionContext.getServletContext().getRealPath(
				"/photo");
		File imgFile = new File(root + "/" + entity.getPicturepath());
		if (imgFile.exists()) {
			imgFile.delete();
		}
		newsphotomanager.delete(id);
			return "delete";
	}
	/**
	 * 按id批量删除
	 */
	public String deleteByIds() {
          for (String id : ids.split(";")) 
          {
	        entity = newsphotomanager.getById(id);
	        String root = ServletActionContext.getServletContext().getRealPath(
			"/photo");
        	File imgFile = new File(root + "/" + entity.getPicturepath());
	        if (imgFile.exists())
	          {
		      imgFile.delete();
         	   }
	        newsphotomanager.delete(id);
	       }
         return "delete";
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new newsphoto();
		} else {
			entity = newsphotomanager.getById(id);
		}
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

}
